package com.zhtools.wx.bean;

import com.zhtools.wx.utils.WXUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信分享返回值
 */
public class WXShareResponse {
    private static final Logger LOGGER = LoggerFactory.getLogger(WXShareResponse.class);

    private static final String SIGNATURE = "SHA-1";

    private static final String SIGNATURE_BEFORE_INFO = ">>> 签名前原始字符串:{}";
    private static final String SIGNATURE_AFTER_INFO = ">>> 签名后字符串:{}";

    private String url;
    private String jsapiTicket;
    private String nonceStr;
    private String timestamp;
    private String signature;
    private String appid;
    private String defaultShareImage;

    public WXShareResponse(String url, String appid, String globalJsapiTicket, String defaultShareImage) {
        this.url = url;
        this.appid = appid;
        this.jsapiTicket = globalJsapiTicket;
        this.nonceStr = WXUtil.generateNonceStr();
        this.timestamp = WXUtil.generateTimestamp();
        this.signature = createSignature();
        this.defaultShareImage = defaultShareImage;
        LOGGER.info(SIGNATURE_AFTER_INFO, this.signature);
    }

    /**
     * 获取签名
     *
     * @return
     */
    private String createSignature() {
        String sKey = "jsapi_ticket=" + this.jsapiTicket
                + "&noncestr=" + this.nonceStr + "&timestamp=" + this.timestamp
                + "&url=" + url;
        LOGGER.info(SIGNATURE_BEFORE_INFO, sKey);
        return getSignature(sKey);
    }

    private String getSignature(String sKey) {
        String ciphertext = "";
        MessageDigest md;
        try {
            md = MessageDigest.getInstance(SIGNATURE);
            byte[] digest = md.digest(sKey.toString().getBytes());
            ciphertext = byteToStr(digest);
            ciphertext = ciphertext.toLowerCase();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ciphertext;
    }

    /**
     * 将字节数组转换为十六进制字符串
     *
     * @param byteArray
     * @return
     */
    private static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符串
     *
     * @param mByte
     * @return
     */
    private static String byteToHexStr(byte mByte) {
        char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];

        String s = new String(tempArr);
        return s;
    }

    public Map<String, String> toMap() {
        Map<String, String> data = new HashMap<>();
        data.put("appid", this.appid);
        data.put("signature", this.signature);
        data.put("timestamp", this.timestamp);
        data.put("nonceStr", this.nonceStr);
        data.put("defaultShareImage", this.defaultShareImage);
        return data;
    }
}
